﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Broker.Upstream
{
    class Program
    {
        static void Main(string[] args)
        {
            StartStockConsole();
        }

        static void StartStockConsole()
        {
            Console.WriteLine("Upstream console started successfully.");
            Console.WriteLine("Type '#quit' to terminate.");
            NotificationService svc = new NotificationService();
            while (true)
            {
                Console.Write(">");
                String name = Console.ReadLine();
                if (String.IsNullOrWhiteSpace(name))
                {
                    Console.WriteLine();
                }
                else if (name.Trim().Equals("#quit"))
                {
                    break;
                }
                else
                {
                    svc.UpdateStock(new Model.Stock() { Name = name });
                }
            }
            Console.WriteLine("Upstream console has been terminated.");
        }

        static void StartStockService()
        {
            ServiceHost host = new ServiceHost(typeof(NotificationService));
            try
            {
                host.Open();
                Console.WriteLine("Upstream service started successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Upstream service failed to start.");
                return;
            }

            Console.WriteLine("Press any key to terminate service...");
            Console.ReadKey();
            host.Close();
            Console.WriteLine("Upstream service has been terminated.");
        }
    }
}
